<?php
namespace App\Http\Controllers\Api\Applet;

use App\Services\AppletUser\UserService;
use Illuminate\Http\Request;

/**
 * 微信用户相关接口
 * @package App\Http\Controllers\Api
 */
class UserBaseController extends BaseController
{
    /**
     * 通过临时登录凭证code换取用户token
     * @param Request $request
     */
    public function getToken(Request $request)
    {
        $appid = $request->header('appid', '');
        $code = $request->code ?? '';
        $token = (new UserService($appid))->code2Session($code);
        $userData = (new UserService($appid))->getUserByToken($token);
        return $this->responseJson(0, ['token' => $token, 'openid' => $userData['openid'] ?? '','unionid'=>$userData['unionid'] ?? '','userInfo' => $userData], '返回成功');
    }
    /**
     *
     * @param Request $request
     */
    public function getUserInfo(Request $request)
    {
        $appid = $request->header('appid', '');
        $token = $request->header('token', '');
        $userData = (new UserService($appid))->getUserByToken($token);
        return $this->responseJson(0, $userData, '请求成功');
    }
    /**
     * 解密数据，专门提供给前端用来解密数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
     */
    public function decrypt(Request $request)
    {
        $appid = $request->header('appid', '');
        $token = $request->header('token', '');
        //接口数据校验
        $validator = \Validator::make($request->all(), [
            'iv' => 'required',
            'encryptedData' => 'required',
        ]);
        if ($validator->fails()) {
            $errors = $validator->errors();
            throw new \Exception($errors->first());
        }
        $encryptedData = $request->input('encryptedData', '');
        $iv = $request->input('iv', '');
        $decryptData = (new UserService($appid))->decryptData($token, $encryptedData, $iv);
        return $this->responseJson(0, ['decryptData' => $decryptData], '请求成功');
    }
    /**
     * 解密用户信息接口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function decryptUserInfo(Request $request)
    {
        $appid = $request->header('appid', '');
        $token = $request->header('token', '');
        //接口数据校验
        $validator = \Validator::make($request->all(), [
            'iv' => 'required',
            'encryptedData' => 'required',
        ]);
        if ($validator->fails()) {
            $errors = $validator->errors();
            throw new \Exception($errors->first());
        }

        $encryptedData = $request->input('encryptedData', '');
        $iv = $request->input('iv', '');

        $userData = (new UserService($appid))->decryptUserData($token, $iv, $encryptedData);
        return $this->responseJson(0, $userData, '请求成功');
    }

    /**
     * 解密用户手机号
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \EasyWeChat\Kernel\Exceptions\DecryptException
     */
    public function decryptUserPhone(Request $request)
    {
        $appid = $request->header('appid', '');
        $token = $request->header('token', '');
        //接口数据校验
        $validator = \Validator::make($request->all(), [
            'iv' => 'required',
            'encryptedData' => 'required',
        ]);
        if ($validator->fails()) {
            $errors = $validator->errors();
            throw new \Exception($errors->first());
        }

        $encryptedData = $request->input('encryptedData', '');
        $iv = $request->input('iv', '');

        $userData = (new UserService($appid))->decryptPhoneData($token, $iv, $encryptedData);
        return $this->responseJson(0, $userData, '请求成功');
    }
}
